<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="人脸标识" prop="faceId">
        <el-input
          v-model="queryParams.faceId"
          placeholder="请输入人脸标识"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备编码" prop="deviceId">
        <el-input
          v-model="queryParams.deviceId"
          placeholder="请输入设备编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--      <el-col :span="1.5">
              <el-button
                type="primary"
                plain
                icon="el-icon-plus"
                size="mini"
                @click="handleAdd"
                v-hasPermi="['viid:image:add']"
              >新增</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="success"
                plain
                icon="el-icon-edit"
                size="mini"
                :disabled="single"
                @click="handleUpdate"
                v-hasPermi="['viid:image:edit']"
              >修改</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="danger"
                plain
                icon="el-icon-delete"
                size="mini"
                :disabled="multiple"
                @click="handleDelete"
                v-hasPermi="['viid:image:remove']"
              >删除</el-button>
            </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['viid:image:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="imageList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="人脸标识" align="center" prop="faceId"/>
      <el-table-column label="图像标识" align="center" prop="imageId"/>
      <el-table-column label="事件分类" align="center" prop="eventSort"/>
      <el-table-column label="设备编码" align="center" prop="deviceId"/>
      <el-table-column label="抓拍图片" align="center" prop="imageUrl">
        <template slot-scope="scope">
          <el-popover placement="top-start" title="" trigger="hover">
            <img :src="scope.row.imageUrl" alt="">
            <img slot="reference" :src="scope.row.imageUrl"
                 style="width: 150px;height: 100px">
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="存储路径" align="center" prop="storagePath"/>
      <el-table-column label="图像文件格式" align="center" prop="fileFormat"/>
      <el-table-column label="宽度" align="center" prop="width"/>
      <el-table-column label="高度" align="center" prop="height"/>
      <el-table-column label="拍摄时间" align="center" prop="shotTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.shotTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="上传时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(scope.row)"
                  v-hasPermi="['viid:image:edit']"
                >修改</el-button>
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.row)"
                  v-hasPermi="['viid:image:remove']"
                >删除</el-button>
              </template>
            </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改人脸照片对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="人脸标识" prop="faceId">
          <el-input v-model="form.faceId" placeholder="请输入人脸标识"/>
        </el-form-item>
        <el-form-item label="图像标识" prop="imageId">
          <el-input v-model="form.imageId" placeholder="请输入图像标识"/>
        </el-form-item>
        <el-form-item label="事件分类：自动分析事件类型，设备采集必选" prop="eventSort">
          <el-input v-model="form.eventSort" placeholder="请输入事件分类：自动分析事件类型，设备采集必选"/>
        </el-form-item>
        <el-form-item label="设备编码" prop="deviceId">
          <el-input v-model="form.deviceId" placeholder="请输入设备编码"/>
        </el-form-item>
        <el-form-item label="存储路径：图像文件的存储路径，采用URI命名规则" prop="storagePath">
          <el-input v-model="form.storagePath" placeholder="请输入存储路径：图像文件的存储路径，采用URI命名规则"/>
        </el-form-item>
        <el-form-item label="图像文件格式" prop="fileFormat">
          <el-input v-model="form.fileFormat" placeholder="请输入图像文件格式"/>
        </el-form-item>
        <el-form-item label="拍摄时间" prop="shotTime">
          <el-date-picker clearable
                          v-model="form.shotTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择拍摄时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="宽度" prop="width">
          <el-input v-model="form.width" placeholder="请输入宽度"/>
        </el-form-item>
        <el-form-item label="高度" prop="height">
          <el-input v-model="form.height" placeholder="请输入高度"/>
        </el-form-item>
        <el-form-item label="图片数据，采用base64加密" prop="data">
          <el-input v-model="form.data" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="minio上传后的图片地址" prop="imageUrl">
          <el-input v-model="form.imageUrl" placeholder="请输入minio上传后的图片地址"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listImage, getImage, delImage, addImage, updateImage} from "@/api/viid/image";

export default {
  name: "Image",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 人脸照片表格数据
      imageList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        faceId: null,
        imageId: null,
        eventSort: null,
        deviceId: null,
        storagePath: null,
        type: null,
        fileFormat: null,
        shotTime: null,
        width: null,
        height: null,
        data: null,
        imageUrl: null,
        orderByColumn: "create_time",
        isAsc: "desc"
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询人脸照片列表 */
    getList() {
      this.loading = true;
      listImage(this.queryParams).then(response => {
        this.imageList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        faceId: null,
        imageId: null,
        eventSort: null,
        deviceId: null,
        storagePath: null,
        type: null,
        fileFormat: null,
        shotTime: null,
        width: null,
        height: null,
        data: null,
        databytes: null,
        imageUrl: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加人脸照片";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getImage(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改人脸照片";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateImage(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addImage(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除人脸照片编号为"' + ids + '"的数据项？').then(function () {
        return delImage(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('viid/image/export', {
        ...this.queryParams
      }, `image_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
